package com.carltechnology.mahjonggamecaculation.views.impl;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.carltechnology.mahjonggamecaculation.R;
import com.carltechnology.mahjonggamecaculation.base.MyApplication;
import com.carltechnology.mahjonggamecaculation.mode.IGameData;
import com.carltechnology.mahjonggamecaculation.presenter.IMahjongDetailPresenter;
import com.carltechnology.mahjonggamecaculation.presenter.impl.MahjongDetailPresenter;
import com.carltechnology.mahjonggamecaculation.utils.views.InfoActionBarView;
import com.carltechnology.mahjonggamecaculation.views.IMahjongDetailView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by WZTCM on 2016/6/28.
 */
public class MahjongDetailActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener,IMahjongDetailView{
    @InjectView(R.id.mahjongdetail_fab)
    FloatingActionButton floatingActionButton;
    @InjectView(R.id.mahjongdetail_detail_toolbar)
    Toolbar toolbar;
    @InjectView(R.id.mahjongdetail_toolbar_layout)
    CollapsingToolbarLayout collapsingToolbarLayout;
    @InjectView(R.id.mahjongdetail_app_bar)
    AppBarLayout appBarLayout;
    @InjectView(R.id.mahjongdetail_item_detail_container)
    NestedScrollView nestedScrollView;
    @InjectView(R.id.mahjongdetail_toolbar_label)
    InfoActionBarView labelInfoBar;
    @InjectView(R.id.mahjongdetail_toolbar_total)
    InfoActionBarView totalInfoBar;
    private String activityLabel;
    private String noThingLabel;
    private boolean isShow = true;
    private int scrollRange = -1;

    private Handler handler = new Handler();
    private IMahjongDetailPresenter presenter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mahjong_item_detail);
        ButterKnife.inject(this);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        //floatingActionButton.hide();

        activityLabel = "麻将";
        noThingLabel = "";

        appBarLayout.addOnOffsetChangedListener(this);
        MahjongDetailFragment fragment = MahjongDetailFragment.newInstance(this);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.mahjongdetail_item_detail_container, fragment)
                .commit();
        final String [] names = MyApplication.getPool().getGameMode().getValues();
        handler.post(new Runnable() {
            @Override
            public void run() {
                labelInfoBar.getPlayer1TextView().setText(names[0]);
                labelInfoBar.getPlayer2TextView().setText(names[1]);
                labelInfoBar.getPlayer3TextView().setText(names[2]);
                labelInfoBar.getPlayer4TextView().setText(names[3]);
                labelInfoBar.getCostTextView().setText(getResources().getString(R.string.mahjong_cost));
                labelInfoBar.getPerunitTextView().setText(getResources().getString(R.string.mahjong_perunit));
            }
        });
        presenter = MahjongDetailPresenter.newInstance(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @OnClick(R.id.mahjongdetail_fab)
    public void onCreateNewGame(View view){
        LayoutInflater inflater = getLayoutInflater();
        final View layout = inflater.inflate(R.layout.dialog_mahjonggame_perunit,
                (ViewGroup) findViewById(R.id.dialog_mahjong_perunit_dialog));
        Dialog dialog = new AlertDialog.Builder(this).setTitle("请输入单价")
                .setView(layout)
                .setNegativeButton("取消",null)
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String text = ((EditText)layout.findViewById(R.id.dialog_mahjong_perunit_input)).getText().toString();
                                MyApplication.getPool().setPerUnit(Integer.valueOf(text));
                                IGameData mode = MyApplication.getPool().getGameMode();
                                mode.update();
                                presenter.updateGameDateBase(mode);
                                Intent intent = new Intent(getActivity(),MahjongRunningGameActivity.class);
                                finish();
                                getActivity().startActivity(intent);
                            }
                        })

                .show();
    }
    private AppCompatActivity getActivity(){
        return this;
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (scrollRange == -1) {
            scrollRange = appBarLayout.getTotalScrollRange();
        }
        if (scrollRange + verticalOffset <= scrollRange/2) {
            // collapsingToolbarLayout.setTitleEnabled(true);
            collapsingToolbarLayout.setTitle(activityLabel);
            floatingActionButton.show();
            isShow = true;
        } else if(isShow) {
            collapsingToolbarLayout.setTitle(noThingLabel);
            floatingActionButton.hide();
            isShow = false;
        }
    }

    @Override
    public void showCaculation(final int[] caculation) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (caculation==null||caculation.length!=5){
                    return ;
                }
                totalInfoBar.getPlayer1TextView().setText(String.valueOf(caculation[0]+"元"));
                totalInfoBar.getPlayer2TextView().setText(String.valueOf(caculation[1]+"元"));
                totalInfoBar.getPlayer3TextView().setText(String.valueOf(caculation[2]+"元"));
                totalInfoBar.getPlayer4TextView().setText(String.valueOf(caculation[3]+"元"));
                totalInfoBar.getCostTextView().setText(String.valueOf(caculation[4]+"元"));
            }
        });
    }
}
